package com.example.middlefront.controller;

import com.example.middlefront.model.Student;
import com.example.middlefront.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    StudentService studentService;

    @GetMapping("/AllStudents")
    public List<Student> getAllStudents(){return studentService.list();}

    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){return studentService.getById(id);}

    @PostMapping("/student")
    public String addStudnet(@RequestBody Student student) {
        if(studentService.save(student)){
            return "添加学生成功";
        }else{
            return "添加学生失败";
        }
    }

    @PutMapping("/student")
    public String updateUser(@RequestBody Student student){
        if(studentService.updateById(student)){
            return "更新学生成功";
        }else{
            return "更新学生失败";
        }
    }

    @DeleteMapping("/student")
    public String deleteStudentById(Integer id){
        if(studentService.removeById(id)){
            return "删除学生成功";
        }else{
            return "删除学生失败";
        }
    }
}
